package com.madrivergroup.mamatodo.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import com.madrivergroup.mamatodo.tasks.Task;
import static com.madrivergroup.mamatodo.util.MaMaConstants.*;

/**
 * Implementation of the ITaskDao, but this is using the SharedPreferences API
 * to store its data.
 * 
 * @author peterdelaney
 *
 */
public class TaskDaoSharedPreferences implements ITaskDao {

	
	public static final String  TASKS     = "TASKS_PREF";
	public static final String  TASK_LIST = "TASK_LIST";
	
	public static final String  NAME = TaskDaoSharedPreferences.class.getSimpleName();

	protected static final String SEPERATOR = "|";
	
	List<Task> listOfTasks = new ArrayList<Task>();
	
	// Need this to get reference to SharedPreference
	protected Application app = null;
	
	public TaskDaoSharedPreferences( Application a ) {
		Log.d(LOG, NAME + ":TaskDaoSharedPreferences( Application )");
		this.app = a;
	}
	public TaskDaoSharedPreferences() {
		Log.d(LOG, NAME + ":TaskDaoSharedPreferences()");		
	}
	
	/**
	 * Add task to our list
	 */
	public void addTask(Task t) {
		Log.d(LOG, NAME + ":addTask( " + t + " ) ");
		saveTask(t);
	} // end of addTask()

	/**
	 * Return list of task
	 */
	public List<Task> getTaskList() {
		Log.d(LOG, NAME + ":getTaskList()");		
		
		listOfTasks.clear();
		
		SharedPreferences   pref  = app.getSharedPreferences(TASKS, 1 ); // MODE_WORLD_READABLE = 1
	
		String tasks = pref.getString( TASK_LIST , "");
		
		List<Task> converted = convertStringToList( tasks ); 
		listOfTasks.addAll( converted );
		Log.i(LOG, "List = " + listOfTasks );
		
		return listOfTasks;
	} // end of getTaskList()

	public List<Task> getTasksByComplete(boolean v) {
		Log.d(LOG, NAME + ":getTasksByComplete( " + v + " )");
	

		List<Task> allList = getTaskList();
		
		List<Task> completeList = new ArrayList<Task>();		
		for( Task task : allList ) {
			if( task.isComplete() == v ) {
				completeList.add( task );
			}
		}
		
		return completeList;
	} // end of getTasksByComplete()

	/**
	 * Not implemented
	 */
	public void removeTask(Task t) {
		Log.d(LOG, NAME + ":removeTask( " + t.getId() + " ) " );
		
		SharedPreferences   pref  = app.getSharedPreferences(TASKS, 2 ); // MODE_WORLD_WRITABLE = 2
		Editor edit  = pref.edit();
		String tasks = pref.getString( TASK_LIST , "");
		Log.i(LOG, "Tasks=" + tasks );

		listOfTasks = convertStringToList( tasks );
		for( Task task : listOfTasks ) {
			if( task.getId() == t.getId() ) {
				Log.i(LOG, "found Task to remove!");
				listOfTasks.remove( task );
				break;
			}
		}

		
		String newTaskString = convertListToString( listOfTasks);
		Log.i( LOG, "New List Saving : " + newTaskString );
		edit.putString(TASK_LIST, newTaskString );
		edit.commit();
		
	} // end of removeTask()

	/**
	 * Save a task
	 */
	public void saveTask(Task t) {
		Log.d(LOG, NAME + ":saveTask( " + t + " ) " );
		
		SharedPreferences   pref  = app.getSharedPreferences(TASKS, 2 ); // MODE_WORLD_WRITABLE = 2
		Editor edit  = pref.edit();
		String tasks = pref.getString( TASK_LIST , "");
		Log.i(LOG, "Tasks=" + tasks );
		if( "".equals( tasks ) == false ) {
			tasks = tasks + SEPERATOR + t.getName();
		}
		else {
			tasks = t.getName();
		}
		Log.i(LOG, "To Save : " + tasks );
		edit.putString(TASK_LIST, tasks );
		edit.commit();
		
		t.setId( determineTaskId() );
		listOfTasks.add( t );
		
		Log.i(LOG, "List After Save : " + listOfTasks );

	} // end of saveTask()

	/**
	 *  NOT IMPLEMENTED
	 */
	public void saveTaskList(List<Task> l) {
		Log.d(LOG, NAME + ":saveTaskList()");
	}

	/**
	 * Determines what the id for a Task should be based on the current set if Ids.
	 * @return
	 */
	protected long determineTaskId() {
		
		long largestId = listOfTasks.size();
		for( Task task : listOfTasks ) {
			if( largestId < task.getId() ) {
				largestId = task.getId();
			}
		}
		
		return largestId + 1;
	}
	/**
	 * Takes a list of tasks separated by comma and converts to a List of
	 * Task objects
	 * 
	 * @param tasks
	 * @return
	 */
	protected List<Task> convertStringToList( String tasks ) {
		Log.d(LOG, NAME + ":convertStringToList( " + tasks + " ) ");
		
		List<Task> list = new ArrayList<Task>();
		StringTokenizer token = new StringTokenizer(tasks, SEPERATOR);
		int index = 0;
		while( token.hasMoreElements() ) {
			String task = token.nextToken();
			Task t = new Task(task);
			t.setId( index++ );
			list.add( t );
		}
		
		return list;
	} // end of convertStringToList()
	
	/**
	 * Converts List to String value
	 * @param list
	 * @return
	 */
	protected String convertListToString( List<Task> list ) {
		Log.d(LOG, NAME + ":convertListToString() ");
		
		StringBuffer buf = new StringBuffer();
		int index = 0;
		for( Task t : list ) {
			String value = (index++ == 0 ) ? t.getName() : SEPERATOR + t.getName();
			buf.append( value );
		}
		
		return buf.toString();
	} // end of convertListToString()

} // end of TaskDaoSharedPreference class
